﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using University.Workshop.Model;
using System.Data;
using System.Collections.ObjectModel;
using System.Windows.Forms;

namespace University.Workshop.DataAccess
{
    public class InvoiceData : IDisposable
    {
        const string connectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=DataBaseHotel;Integrated Security=True";
        SqlConnection conex;
        public void RegisterInvoice(Invoice invoice)
        {
            if (invoice == null)
                throw new ArgumentNullException("invoice");

            try
            {
                conex = new SqlConnection(connectionString);
                SqlCommand cmd = conex.CreateCommand();
                cmd.CommandText = @"INSERT INTO [DataBaseHotel].[dbo].[Invoice]
                                           ([Nro]
                                           ,[NIT/CI]
                                           ,[FullName]
                                           ,[Date])
                                     VALUES (@Id
                                            ,@Nit
                                            ,@FullName
                                            ,@Date)";
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@Id", invoice.Id);
                cmd.Parameters.AddWithValue("@Nit", invoice.CI);
                cmd.Parameters.AddWithValue("@FullName", invoice.FullName);
                cmd.Parameters.AddWithValue("@Date", invoice.Date);
                conex.Open();
                cmd.ExecuteNonQuery();
                cmd.CommandText = @"INSERT INTO [DataBaseHotel].[dbo].[InvoiceDetail]
                                             ([Nro]
                                             ,[Detail]
                                             ,[Mount])
                                       VALUES(@Nro
                                             ,@Detail
                                             ,@Mount)";
                foreach (InvoiceDetail item in invoice.Detail)
                {
                    cmd.Parameters.AddWithValue("@Nro", invoice.Id);
                    cmd.Parameters.AddWithValue("@Detail", item.Detail);
                    cmd.Parameters.AddWithValue("@Mount", item.Mount);
                    cmd.ExecuteNonQuery();
                    cmd.Parameters.RemoveAt("@Nro");
                    cmd.Parameters.RemoveAt("@Detail");
                    cmd.Parameters.RemoveAt("@Mount");
                }
            }
            catch (Exception ex)
            {
                throw new ArgumentException(ex.Message);
            }
            conex.Close();
        }
       
         public void GetEmployeeInvoice(DataGridView employee) 
        {
            if (employee == null)
                throw new ArgumentNullException("employee");
            conex = new SqlConnection(connectionString);
            SqlCommand cmd = conex.CreateCommand();
            cmd.CommandText = "SELECT [CI],[Name],[workArea] FROM [DataBaseHotel].[dbo].[Employee]";
            cmd.CommandType = CommandType.Text;
            if (cmd.Connection.State == ConnectionState.Closed)
            {
                cmd.Connection.Open();
            }
            SqlDataReader reader = cmd.ExecuteReader();
            int index = 0;
            try
            {
                while (reader.Read())
                {
                    employee.Rows.Add();
                    employee[0, index].Value = reader["CI"].ToString();
                    employee[1, index].Value = reader["Name"].ToString();
                    switch (reader["workArea"].ToString())
                    {
                        case "Concina":
                            employee[2, index].Value = "1558";
                            break;
                        case "Lavanderia":
                            employee[2, index].Value = "1750";
                            break;
                        case "Servicio a la Habitacion":
                            employee[2, index].Value = "1492";
                            break;
                        case "Limpieza":
                            employee[2, index].Value = "1580";
                            break;
                        default:
                            employee[2, index].Value = "1200";
                            break;
                    }
                    ++index;
                }
            }
            catch (Exception ex)
            {

                throw new ArgumentException(ex.Message);
            }
        }
        #region IDisposable Members
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (conex != null)
                {
                    conex.Dispose();
                    conex = null;
                }
            }
        }
        #endregion
    }
}
